package com.llu.ssrf;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class BaseTest {

    public static void main(String[] args) {
        testHttpURLConnection();
    }


    /**
     * @description: 测试URLConnection
     * Java 网络编程 流、网络
     */
    public static void testURLConnect(){
        try {
            // 创建一个URL对象，指向需要访问的网页地址
            URL url = new URL("file:///E:\\code\\Java代码审计系统学习\\CodeReviewStudy\\docker-compose.yml");
            // 通过URL对象打开一个连接
            URLConnection connection = url.openConnection();
            // 实际执行连接操作
            connection.connect();

            // 获取连接后的输入流，用于读取网页内容
            InputStream inputStream = connection.getInputStream();
            // 使用BufferedReader读取输入流中的内容，这样可以按行读取
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            // 循环读取每一行，直到没有内容为止
            while ((line = reader.readLine()) != null) {
                // 输出读取的每一行内容
                System.out.println(line);
            }
            // 关闭BufferedReader以释放资源
            reader.close();
        } catch (Exception e) {
            // 捕获并处理所有异常
            e.printStackTrace();
        }

    }

    /**
     * @description: 测试HttpURLConnection
     */
    public static void testHttpURLConnection(){
        try {
            // 创建URL对象，指向目标网站
//            URL url = new URL("http://www.baidu.com");
            URL url = new URL("file:///E:\\code\\Java代码审计系统学习\\CodeReviewStudy\\docker-compose.yml");

            // 打开与URL的连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为GET
            connection.setRequestMethod("GET");
            // 建立与URL的实际连接
            connection.connect();

            // 获取服务器响应码
            int responseCode = connection.getResponseCode();
            // 打印响应码
            System.out.println("Response Code: " + responseCode);

            // 获取服务器响应的输入流
            InputStream inputStream = connection.getInputStream();
            // 包装输入流，以便按行读取
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            // 读取并打印每一行响应内容
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            // 关闭输入流
            reader.close();
        } catch (Exception e) {
            // 处理异常
            e.printStackTrace();
        }


    }

    /**
     * @description: 测试HttpClient
     */
    public static void testHttpClient(){

        // 定义要访问的URL地址，指向百度首页
        String url = "https://www.baidu.com";

        // 尝试使用CloseableHttpClient发起HTTP请求，并确保在操作完成后正确关闭资源
        try (
                CloseableHttpClient httpClient = HttpClients.createDefault() // 创建默认的HttpClient实例
        ) {
            // 创建HttpGet请求，指定请求的URL
            HttpGet request = new HttpGet(url);
            // 尝试执行HTTP请求，并确保在操作完成后正确关闭响应资源
            try (
                    CloseableHttpResponse response = httpClient.execute(request)
            ) {
                HttpEntity entity = response.getEntity(); // 获取响应实体
                // 如果响应实体存在，则打印响应内容
                if (entity != null) {
                    String result = EntityUtils.toString(entity); // 将响应实体转换为字符串
                    System.out.println(result); // 打印响应内容
                }
            }
        } catch (IOException e) {
            e.printStackTrace(); // 如果在执行过程中发生IO异常，则打印异常信息
        }

    }

    /**
     * @description: 测试openStream
     */
    public static void testOpenStream(){
        try {
            URL url = new URL("https://www.baidu.com");
            InputStream inputStream = url.openStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //test ImageIO
    public static void testImageIO(){
        try {
            // 从本地文件读取图像
//            File file = new File("path/to/image.png");
//            BufferedImage imageFromFile = ImageIO.read(file);
//            System.out.println("Image read from file: " + imageFromFile);

            // 从URL读取图像
            URL url = new URL("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png");
            BufferedImage imageFromURL = ImageIO.read(url);
            System.out.println("Image read from URL: " + imageFromURL);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void testOkhttp() {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("http://www.baidu.com")
                .build();

        try (Response response = client.newCall(request).execute()) {
            System.out.println("Response Code: " + response.code());
            System.out.println("Response Body: " + response.body().string());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
